package com.ss.android.article.browser.constant;

import android.app.Application;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.ss.android.article.browser.a.a;
import com.ss.android.article.browser.c.g;
import com.ss.android.article.browser.database.BrowserHistory;
import com.ss.android.article.browser.database.bookmark.m;
import com.ss.android.common.app.AbsApplication;

import java.io.Closeable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import inline.FileUtils;

public final class b {
    @NonNull
    final Bitmap a;
    Application b;
    m c;
    a d;
    @NonNull
    private final String e;

    public b(@NonNull Context arg3) {
        this.b = AbsApplication.getApplication();
        this.c = com.ss.android.article.browser.database.bookmark.a.a();
        this.d = com.ss.android.article.browser.a.a.a();
        this.a = g.a(arg3);
        this.e = this.b.getString(com.ss.android.article.browser.R.string.bf);
    }

    @NonNull
    static File a(@NonNull Application arg3) {
        return new File(arg3.getCacheDir(), "folder.png");
    }

    @NonNull
    public static File a(@NonNull Application arg4, @Nullable String arg5) {
        String v0 = !TextUtils.isEmpty(((CharSequence) arg5)) ? arg5 + '-' : "";
        return new File(arg4.getFilesDir(), v0 + "bookmarks.html");
    }

    static void a(Bitmap arg3, File arg4) {
        Closeable v1_1;
        FileOutputStream v1 = null;
        Closeable v2 = null;
        try {
            v1 = new FileOutputStream(arg4);
            arg3.compress(Bitmap.CompressFormat.PNG, 100, ((OutputStream) v1));
            arg3.recycle();
        } catch (FileNotFoundException v0_1) {
            v0_1.printStackTrace();
        } finally {
            FileUtils.a(v1);
            return;
        }
    }

    static void a(b arg10, List arg11, String arg12) {
        Closeable v1_4;
        FileWriter v1_3 = null;
        String v1_2;
        File v3 = a(arg10.b, arg12);
        StringBuilder v4 = new StringBuilder("<!DOCTYPE html><html xmlns=http://www.w3.org/1999/xhtml>\n" +
                "<head>\n" +
                "<meta content=en-us http-equiv=Content-Language />\n" +
                "<meta content=\'text/html; charset=utf-8\' http-equiv=Content-Type />\n" +
                "<meta name=viewport content=\'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no\'>\n" +
                "<title>" + arg10.e + "</title>\n" +
                "</head>\n" +
                "<style>body{background: #E5E5E5; padding-top: 5px;max-width:100%;min-height:100%}#content{width:100%;max-width:800px;margin:0 auto;text-align:center}.box{vertical-align:middle;text-align:center;position:relative;display:inline-block;height:45px;width:150px;margin:6px;padding:4px;background-color:#fff;border: 1px solid #d2d2d2;border-top-width: 0;border-bottom-width: 2px;font-family:Arial;color:#444;font-size:12px;-moz-border-radius:2px;-webkit-border-radius:2px;border-radius:2px}.box-content{height:25px;width:100%;vertical-align:middle;text-align:center;display:table-cell}p.ellipses{width:130px;font-size: small;font-family: Arial, Helvetica, \'sans-serif\';white-space:nowrap;overflow:hidden;text-align:left;vertical-align:middle;margin:auto;text-overflow:ellipsis;-o-text-overflow:ellipsis;-ms-text-overflow:ellipsis}.box a{width:100%;height:100%;position:absolute;left:0;top:0}img{vertical-align:middle;margin-right:10px;width:20px;height:20px;}.margin{margin:10px}</style>\n" +
                "<body>" +
                "<div id=content>");
        String v5 = a(arg10.b).toString();
        int v6 = arg11.size();
        int v2;
        for (v2 = 0; v2 < v6; ++v2) {
            Object v0 = arg11.get(v2);
            v4.append("<div class=box><a href=\'");
            if (((BrowserHistory) v0).h) {
                v4.append("file://").append(a(arg10.b, ((BrowserHistory) v0).title));
                v4.append("\'></a>\n<div class=margin>\n<div class=box-content>\n<p class=ellipses>\n<img src=\'");
                v4.append(v5);
                arg10.a(((BrowserHistory) v0).title);
            } else {
                Uri v1 = android.support.design.a.t(((BrowserHistory) v0).url);
                if (v1 != null) {
                    File v1_1 = com.ss.android.article.browser.a.a.a(arg10.b, v1);
                    if (!v1_1.exists()) {
                        arg10.d.a(arg10.d.a(((BrowserHistory) v0).title), ((BrowserHistory) v0).url).a();
                    }

                    v1_2 = "file://" + v1_1;
                } else {
                    v1_2 = "file://" + b(arg10.b);
                }

                v4.append(((BrowserHistory) v0).url);
                v4.append("\'></a>\n<div class=margin>\n<div class=box-content>\n<p class=ellipses>\n<img src=\'").append(v1_2);
            }

            v4.append("\' />");
            v4.append(((BrowserHistory) v0).title);
            v4.append("</p></div></div></div>");
        }

        v4.append("</div></body></html>");
        try {
            v1_3 = new FileWriter(v3, false);
            v1_3.write(v4.toString());
        } catch (IOException v0_2) {
            v0_2.printStackTrace();
        } finally {
            FileUtils.a(v1_3);
            return;
        }
    }

    final void a(@Nullable String arg3) {
        this.c.c(arg3).a(new d(this, arg3));
    }

    @NonNull
    static File b(@NonNull Application arg3) {
        return new File(arg3.getCacheDir(), "default.png");
    }
}

